<template>
	<view class="main">
		<view class="mains">
			<u-sticky>
				<view class="top_c">
					<view class="top">
						<view class="items">
							<view class="lefts">
								<text>代理</text>
							</view>
							<view class="rights">
								<text>{{agentData.name}}</text>
								<text class="phone">{{agentData.phone}}</text>
							</view>
						</view>
						<view class="items">
							<view class="lefts">
								<text>产品</text>
							</view>
							<view class="rights" @click="proShow = true">
								<u-icon size="24" space="8px" :name="product_image"></u-icon>
								<text class="products">{{product_name}}</text>
								<u-icon size="24" space="8px" color="#1296db" name="list"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</u-sticky>
			<view class="content">
				<view class="types">
					<view class="lists">
						<view class="title">
							<text>是否押金</text>
						</view>
						<view class="items">
							<view class="radios">
								<u-radio-group v-model="deposit_types" @change="depositTypeChange" placement="row"
									iconPlacement="left">
									<u-radio :customStyle="{marginRight: '20px'}"
										v-for="(item, index) in deposit_type_list" :key="index" :label="item.name"
										:name="item.id" activeColor="red"></u-radio>
								</u-radio-group>
							</view>
						</view>
					</view>
				</view>
				<view class="types">
					<view v-if="toTradesForm && toTradesForm.length > 0" class="lists standard">
						<view class="title">
							<text>分润配置</text>
						</view>
						<view class="list-item" v-for="(item,index) in toTradesForm" :key="index">
							<view class="title">
								<text>分润配置【{{index + 1}}】</text>
							</view>
							<view class="dayFrame">
								<view class="dayFrame-lefts">
									<text>时间段</text>
								</view>
								<view class="dayFrame-rights">
									<view class="lefts">
										<input type="text" placeholder="开始天数" v-model="item.start_day" disabled="true">
									</view>
									<view class="centers">
										<text>~</text>
									</view>
									<view v-if="item.end_day < 3650" class="rights">
										<input type="text" placeholder="结束天数" v-model="item.end_day" disabled="true">
									</view>
									<view class="days">
										<text v-if="item.end_day < 3650">天</text>
										<text v-else>长期</text>
									</view>
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									<text>贷记卡结算费率</text>
								</view>
								<view class="rights">
									<input type="text" :placeholder="item.min_money + ' ~ ' + item.max_money"
										v-model="item.money">
									<text>‱</text>
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									<text>扫码结算费率</text>
								</view>
								<view class="rights">
									<input type="text" :placeholder="item.scan_min_money + ' ~ ' + item.scan_max_money"
										v-model="item.scan_money">
									<text>‱</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="placeholders"></view>
			<view class="saveBtn" @click="saveData">
				<text>提交</text>
			</view>
			<u-picker :show="proShow" :columns="proColumns" keyName="label" @confirm="proConfirm"
				@cancel="proShow = false"></u-picker>
		</view>
	</view>
</template>

<script>
	import tabbarItem from '../../../uni_modules/uview-ui/libs/config/props/tabbarItem'
	var util = require('@/util/request.js');

	export default {
		data() {
			return {
				agent_id: 0,
				agentData: {},
				proShow: false,
				proColumns: [],
				product_id: '',
				product_name: '',
				product_image: '',
				myTradesForm: null,
				toTradesForm: null,
				agentPolicy: null,
				settings: 1,
				is_true: true,
				title: '',
				data: [],
				policyMsg: '确定要提交吗？',
				deposit_type: 1,
				deposit_types: 1,
				deposit_type_list: [{
					id: 1,
					name: '无押'
				}, {
					id: 2,
					name: '押金'
				}],
			}
		},
		onLoad(e) {
			this.agent_id = e.id
			this.getProduct()
			this.getAgent()
		},
		methods: {
			// 获取代理
			async getAgent() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'user/getAgent',
					data: {
						agent_id: this.agent_id
					}
				});
				if (res.code == 200) {
					this.agentData = res.data
				}
			},
			// 获取产品
			async getProduct() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'datas/getHzyProduct',
					data: {
						type: 2
					}
				});
				if (res.code == 200) {
					this.proColumns = [res.data]
					this.product_id = res.data[0].id
					this.product_name = res.data[0].label
					this.product_image = this.API_URL + res.data[0].images
					this.loadData()
				}
			},
			// 获取数据
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getAgentHzyPolicy',
					data: {
						deposit_type: this.deposit_type,
						product_id: this.product_id,
						agent_id: this.agent_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.data = res.data
					if (res.data.myPolicy && res.data.myPolicy.trades && res.data.myPolicy != null) {
						this.myTradesForm = res.data.myPolicy.trades
					}
					if (res.data.agentPolicy && res.data.agentPolicy != null && res.data.agentPolicy.trades) {
						this.toTradesForm = res.data.agentPolicy.trades
						this.agentPolicy = res.data.agentPolicy
					}
					this.settings = res.data.settings
				}
			},
			// 押金选择
			depositTypeChange(index) {
				if (this.deposit_type != index) {
					this.deposit_type = index
					this.deposit_types = index
					this.loadData()
				}
			},
			// 保存数据
			saveData() {
				var _this = this
				// 交易分润 - 验证
				for (var i in this.toTradesForm) {
					if (this.toTradesForm[i].money == '' || this.toTradesForm[i].money == null) {
						uni.showToast({
							title: '请设置分润配置【' + (Number(i) + 1) + '】 - 贷记卡结算费率！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toTradesForm[i].money * 100 < this.toTradesForm[i]
						.min_money * 100 || this.toTradesForm[i].money * 100 > this.toTradesForm[i]
						.max_money * 100) {
						uni.showToast({
							title: '分润配置【' + (Number(i) + 1) + '】 - 贷记卡结算费率超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toTradesForm[i].scan_money == '' || this.toTradesForm[i].scan_money == null) {
						uni.showToast({
							title: '请设置分润配置【' + (Number(i) + 1) + '】 - 扫码结算费率！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.toTradesForm[i].scan_money * 100 < this.toTradesForm[i]
						.scan_min_money * 100 || this.toTradesForm[i].scan_money * 100 > this.toTradesForm[i]
						.scan_max_money * 100) {
						uni.showToast({
							title: '分润配置【' + (Number(i) + 1) + '】 - 扫码结算费率超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				uni.showModal({
					content: this.policyMsg,
					success: (res) => {
						if (res.confirm) {
							_this.saveDataFun()
						}
					}
				})
			},
			// 保存数据 - 方法
			async saveDataFun() {
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp({
						url: 'v1/saveAgentHzyPolicy',
						data: {
							product_id: this.product_id,
							deposit_type: this.deposit_type,
							trades: this.toTradesForm,
							settings: this.settings,
							agent_id: this.agent_id,
							id: this.agentPolicy.id
						}
					});
					uni.hideLoading()
					this.is_true = true
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
					if (res.code == 200) {
						// setTimeout(function() {
						// 	uni.navigateBack({});
						// }, 1000)
					}
				}
			},
			// 选择产品
			proConfirm(e) {
				this.product_id = e.value[0].id
				this.product_name = e.value[0].label
				this.product_image = this.API_URL + e.value[0].images
				this.proShow = false
				uni.showLoading({
					title: '加载中'
				});
				this.myTradeForm = null
				this.toTradeForm = null
				this.loadData()
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}

	input {
		font-size: 28rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		// width: 200rpx;
		background-color: #DCDFE6;
	}

	.main {
		.mains {

			.top_c {
				padding: 30rpx;
				background-color: #F2F6FC;

				.top {
					padding: 0 30rpx;
					background-color: #ffffff;
					border-radius: 18rpx;
					box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

					.items {
						padding: 30rpx 0;
						display: flex;
						justify-content: space-between;
						border-bottom: 1px solid #EBEEF5;

						.lefts {}

						.rights {
							display: flex;
							align-items: center;

							.phone {
								padding-left: 16rpx;
							}

							.products {
								padding-right: 16rpx;
							}
						}
					}
				}

				.tags {
					margin-top: 30rpx;
				}
			}

			.content {
				padding: 0 30rpx;

				.types {

					.lists {
						padding: 30rpx 30rpx 0 30rpx;
						background-color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
						margin-bottom: 30rpx;

						.title {
							text-align: center;
							padding-bottom: 30rpx;
							border-bottom: 1px solid #EBEEF5;
							font-weight: bold;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.rights {
								display: flex;
								align-items: center;

								.setZore {
									padding: 4rpx 18rpx;
									font-size: 24rpx;
									background-color: #fa3534;
									border-radius: 8rpx;
									color: #ffffff;
									font-weight: normal;
									margin-right: 16rpx;
								}
							}
						}

						.items {
							display: flex;
							justify-content: space-between;
							align-items: center;
							padding: 20rpx 0;
							border-top: 1px solid #EBEEF5;

							.radios {
								width: 100%;
								padding-top: 10rpx;
								padding-bottom: 20rpx;
							}

							.rights {
								display: flex;
								justify-content: flex-end;
								align-items: center;

								input {
									width: 70%;
									text-align: center;
									padding-right: 60rpx;
								}

								text {
									position: absolute;
									padding-right: 10rpx;
								}

								.flow_unit {
									padding-right: 80rpx;
								}
							}
						}

						.items-add {
							border-top: 1px solid #EBEEF5;
							padding: 20rpx 0rpx;
							color: #2979ff;

							.add-btn {
								display: flex;
								justify-content: center;
								align-items: center;
								text-align: center;

								text {
									padding-left: 10rpx;
								}
							}
						}

						.list-item {
							margin-top: 30rpx;
							border-radius: 10rpx;
							border: 1px dashed #c8c9cc;
							padding: 20rpx 20rpx 0 20rpx;

							.dayFrame {
								padding: 20rpx 0;
								display: flex;
								justify-content: space-between;
								border-bottom: 1px solid #EBEEF5;
								align-items: center;
								text-align: center;

								.dayFrame-lefts {
									width: 30%;
								}

								.dayFrame-rights {
									display: flex;
									justify-content: flex-end;
									align-items: center;

									.lefts {
										font-size: 28rpx;
										width: 30%;
									}

									.centers {
										width: 10%;
									}

									.rights {
										width: 30%;
										display: flex;
										justify-content: flex-end;
									}

									.days {
										padding-left: 10rpx;
										font-size: 28rpx;
										color: #909399;
									}
								}
							}
							
							.items {
								.lefts {
									font-size: 28rpx;
									width: 50%;
								}
							}
						}

						.list-item:last-child {
							margin-bottom: 30rpx;
						}
					}

					.standard {
						padding-bottom: 10rpx;
					}

					.lists-add {
						padding: 30rpx 0rpx;
						background-color: #2979ff;
						color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

						.add-btn {
							display: flex;
							justify-content: center;
							align-items: center;
							text-align: center;

							text {
								padding-left: 10rpx;
							}
						}
					}
				}
			}

			.placeholders {
				height: 90rpx;
			}

			.saveBtn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				text-align: center;
				background-color: #409EFF;
				color: #ffffff;
				padding: 20rpx 0;
			}
		}
	}
</style>